package leetcode.hot100;

import java.util.Arrays;

public class Solution3 {

    public static void main(String[] args) {
        Solution3 s = new Solution3();
        String ss = "pwwkew";
        System.out.println(ss.substring(0,2));
    }


    public int lengthOfLongestSubstring(String s) {
        //存储上次字符出现的位置
        int[] lastPositions = new int[128];
        Arrays.fill(lastPositions, -1);
        //保存迭代值
        int preLen = 0, curLen = 0, maxLen = 0;
        for (int i = 0; i < s.length(); i++) {
            //迭代
            if(lastPositions[s.charAt(i)]<i-preLen){
                curLen = preLen + 1;
            }else{
                curLen = i - lastPositions[s.charAt(i)];
            }
            //交换值
            lastPositions[s.charAt(i)] = i;
            maxLen = maxLen<curLen?curLen:maxLen;
            preLen = curLen;
        }
        return maxLen;
    }
}
